package com.example.minefieldkeepercloud.activity.machineslist;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.minefieldkeepercloud.R;

import com.example.minefieldkeepercloud.bean.BaseBean;
import com.example.minefieldkeepercloud.bean.YHByBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.dialog.BottomDialog;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 矿机维护
 */
public class MchineStateAc extends AppCompatActivity {
    private TimePickerView pvTimeDay;//年月日时间选择器
    private SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
    private String NowData="";
    private String factoryID="";
    private String kind="";
    private String MachineName1="";
    private String IP="";
    private BottomDialog bottomDialog;
    private String UserID="",FullName="";
    @BindView(R.id.tv_ip)
    TextView tv_ip;
    @BindView(R.id.tv_p)
    TextView tv_p;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.title_center)
    TextView title_center;
    @BindView(R.id.edit_content)
    EditText edit_content;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_machinestate);
        ButterKnife.bind(this);
        CreateTime();
        getUser();
    }

    private void CreateTime()
    {
        //时间选择器
        pvTimeDay = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置范围
        pvTimeDay.setRange(2017, 2050);

        //设置是否循环滚动
        pvTimeDay.setCyclic(false);
        pvTimeDay.setCancelable(true);

        //时间选择后回调
        pvTimeDay.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                NowData=ymd.format(date);
                tv_time.setText(NowData);

            }


        });
        pvTimeDay.setTime(new Date(System.currentTimeMillis()));


        factoryID=getIntent().getBundleExtra("bd").getString("factoryID");
        kind=getIntent().getBundleExtra("bd").getString("kind");
        MachineName1=getIntent().getBundleExtra("bd").getString("MachineName1");
        IP=getIntent().getBundleExtra("bd").getString("IP");

        if(kind.equals("4"))
            title_center.setText("矿机维护");
        else
            title_center.setText("矿机维修");

        tv_ip.setText(IP);
    }

    @OnClick({R.id.title_left,R.id.ly_person,R.id.ly_time,R.id.btn_post})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.ly_person:
                if(bottomDialog!=null)
                bottomDialog.show();
                break;
            case R.id.ly_time:
                pvTimeDay.show();
                break;
            case R.id.btn_post:
                Post(edit_content.getText().toString());
                break;
        }
    }

    /**
     * 获取用户
     */
    private void getUser() {
        if (!NetWorkUtil.check(this)) {
            return;
        }
        NetHelperNew.GetYHByFactory(factoryID,new ApiHttpCallback(MchineStateAc.this, 1,true) {
            @Override
            public void onSuccess(String data) {
                YHByBean yhByBean = MyGson.getInstance().fromJson(data, YHByBean.class);
                if (yhByBean.getType() == 1) {
                    if (yhByBean.getData().size() > 0) {
                         bottomDialog=new BottomDialog(MchineStateAc.this, yhByBean, new BottomDialog.OnSelectListener() {
                             @Override
                             public void select(int id, String name) {
                                 UserID=id+"";
                                 FullName=name;
                                 tv_p.setText(name);
                             }
                         });
                        FullName=yhByBean.getData().get(0).getFullName();
                        UserID=yhByBean.getData().get(0).getID()+"";
                        tv_p.setText(FullName);
                    } else {
                        ToastUtil.showToast(MchineStateAc.this, "没有可查询的处理人");
                    }

                } else {
                    finish();
                    ToastUtil.showToast(MchineStateAc.this, yhByBean.getContent());
                }
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(MchineStateAc.this, msg);
                finish();
            }
        });


    }

    private void Post(String Remark)
    {
        if (!NetWorkUtil.check(this)) {
            return;
        }
        NetHelperNew.UpdateMachineErrInfoFix(kind, factoryID, IP, MachineName1, UserID,
                 FullName, Remark,NowData,new ApiHttpCallback(MchineStateAc.this, 1,true) {
            @Override
            public void onSuccess(String data) {
                BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                if (baseBean.getType() == 1) {
                    finish();
                }
                ToastUtil.showToast(MchineStateAc.this, baseBean.getContent());
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(MchineStateAc.this, msg);

            }
        });

    }


}
